/*
 * Copyright (C) 2010 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.gobygang.client.facebook;

import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.ScriptElement;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;

/** Manages creating {@link FBConnection}s to receive messages from the server. */
public class FacebookFactory {

	private static final String FB_SRC = "http://connect.facebook.net/en_US/all.js";
	private static boolean scriptLoaded = false;
	private static AsyncCallback<ConnectState> thisCallback;

	public enum Action{WAIT,LOGIN};
	private static Command thisCommand;
	//public static FBConnection createFBConnection() {
	public static void createFBConnection(AsyncCallback<ConnectState> callback, Command command) {
		thisCallback = callback;
		thisCommand = command;
		//Log.info("fb load begin: scriptLoaded="+scriptLoaded);

		if (!scriptLoaded) {
			ScriptElement script = Document.get().createScriptElement();
			script.setSrc(FB_SRC);
			Document.get().getElementsByTagName("head").getItem(0).appendChild(script);
			addOnLoadHandler(script);
			waitAWhile(0,command);

		} else {
			//Log.info("fbApiReady");
			fbApiReady();
		}

		//return createChannelImpl(channelId);
	}

	public static native void addOnLoadHandler(ScriptElement script)/*-{
    		script.onload = function() {
           		@com.gobygang.client.facebook.FacebookFactory::fbApiReady()();
			}
			// IE 6 & 7
			script.onreadystatechange = function() {
			if (this.readyState == 'complete') {
           		@com.gobygang.client.facebook.FacebookFactory::fbApiReadyIE()();
			}
			}

    	}-*/;

	public static  void fbApiReadyIE(){
		//Log.info("internet explorer fb api ready");
		fbApiReady();
	}
	public static  void fbApiReady(){
		//Log.info("fb api ready");
		scriptLoaded = true;

		thisCommand.execute();
		//FacebookApi.waitForAPI(new GetUserDetailsCallback());

	}

	private static void  waitAWhile(final int counter, final Command command){
		Timer timer = new Timer() {
			public void run() {
				//Log.info("waitAWhile "+counter+" "+scriptLoaded);
				if (scriptLoaded){
					return;
				}
				int i = counter + 1;
				if (i > 8){
					//Log.info("timed out "+scriptLoaded);
					if (!scriptLoaded){
						createFBConnection(thisCallback,command);
					}
					return;
				}
				waitAWhile(i,command);
			}
		};
		timer.schedule(500);
	}

}

